/*
 * Copyright 2012 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.chacha.client;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.chacha.client.command.Response;
import org.chacha.client.command.account.AddAccountRequest;
import org.chacha.client.command.account.DelAccountRequest;
import org.chacha.client.command.account.ModifyAccountRequest;
import org.chacha.client.command.account.QueryAccountRequest;
import org.chacha.client.command.account.QueryAccountResponse;
import org.chacha.client.command.auth.LoginRequest;
import org.chacha.client.command.auth.LoginResponse;
import org.chacha.client.command.auth.TokenLogout;
import org.chacha.client.command.auth.TokenRequest;
import org.chacha.exception.ChachaException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;

/**
 * 
 *	@author: hejing
 *	@date: 2012-11-30-下午04:29:47
 */
public class SocketServiceImpl implements CommandService{
	private InetSocketAddress[] hosts;	//服务器集合
	private Sender sender;
	
	public SocketServiceImpl(InetSocketAddress host){
		this(new InetSocketAddress[]{host});
	}
	
	public SocketServiceImpl(InetSocketAddress[] hosts) {
		this.hosts = hosts;
		sender=new SocketSender(this.hosts);
	}

	@Override
	public LoginResponse loginPassword(LoginRequest loginRequest) throws ChachaException{
		LoginResponse r=new LoginResponse();
		try {
			String request = loginRequest.toJson();
			String response=sender.syncRequest(request);
			r.fromJson(response);
		} catch (JsonMappingException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		}
		return r;
	}

	@Override
	public LoginResponse loginToken(TokenRequest tokenRequest) throws ChachaException {
		LoginResponse r=new LoginResponse();
		try {
			String request = tokenRequest.toJson();
			String response=sender.syncRequest(request);
			r.fromJson(response);
		} catch (JsonMappingException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		}
		return r;
	}

	@Override
	public LoginResponse logout(TokenLogout tokenLogout) throws ChachaException {
		LoginResponse r=new LoginResponse();
		try {
			String request = tokenLogout.toJson();
			String response=sender.syncRequest(request);
			r.fromJson(response);
		} catch (JsonMappingException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		}
		return r;
	}

	@Override
	public Response addAccount(AddAccountRequest addRequest)
			throws ChachaException {
		Response r=new Response();
		try {
			String request=addRequest.toJson();
			String response=sender.syncRequest(request);
			r.fromJson(response);
		} catch (JsonMappingException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		}
		return r;
	}

	@Override
	public Response modifyAccount(ModifyAccountRequest modifyRequest)
			throws ChachaException {
		Response r=new Response();
		try {
			String request=modifyRequest.toJson();
			String response=sender.syncRequest(request);
			r.fromJson(response);
		} catch (JsonMappingException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		}
		return r;
	}

	@Override
	public Response delAccount(DelAccountRequest delRequest)
			throws ChachaException {
		Response r=new Response();
		try {
			String request=delRequest.toJson();
			String response=sender.syncRequest(request);
			r.fromJson(response);
		} catch (JsonMappingException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		}
		return r;
	}

	@Override
	public QueryAccountResponse queryAccount(QueryAccountRequest queryRequest)
			throws ChachaException {
		QueryAccountResponse r=new QueryAccountResponse();
		try {
			String request=queryRequest.toJson();
			String response=sender.syncRequest(request);
			r.fromJson(response);
		} catch (JsonMappingException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new ChachaException(e);
		}
		return r;
	}
}
